package com.heima.wemedia.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.constants.WemediaConstants;
import com.heima.common.redis.CacheService;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmSensitiveChannelPageDto;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.utils.common.SensitiveWordUtil;
import com.heima.wemedia.mapper.WmSensitiveMapper;
import com.heima.wemedia.service.WmSensitiveService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class WmSensitiveServiceImpl extends ServiceImpl<WmSensitiveMapper, WmSensitive> implements WmSensitiveService {
    @Autowired
    private CacheService cacheService;

    /**
     * 敏感词列表显示
     *
     * @param wmSensitivePageDto
     * @return
     */
    @Override
    public ResponseResult sensitiveList(WmSensitiveChannelPageDto wmSensitivePageDto) {
        if (wmSensitivePageDto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        wmSensitivePageDto.checkParam();
        IPage page = new Page(wmSensitivePageDto.getPage(), wmSensitivePageDto.getSize());

        LambdaQueryWrapper<WmSensitive> query = new LambdaQueryWrapper<>();

        if (StringUtils.isNotEmpty(wmSensitivePageDto.getName())) {
            query.eq(WmSensitive::getSensitives, wmSensitivePageDto.getName());
        }
        query.orderByDesc(WmSensitive::getCreatedTime);
        page(page, query);

        PageResponseResult responseResult = new PageResponseResult(wmSensitivePageDto.getPage(), wmSensitivePageDto.getSize(), (int) page.getTotal());

        return responseResult.ok(page.getRecords());
    }

    /**
     * 保存敏感词
     *
     * @param wmSensitive
     * @return
     */
    @Override
    public ResponseResult saveSensitive(WmSensitive wmSensitive) {
        if (wmSensitive == null || StringUtils.isEmpty(wmSensitive.getSensitives())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        WmSensitive wmSensitiveNew = getOne(Wrappers.<WmSensitive>lambdaQuery().eq(WmSensitive::getSensitives, wmSensitive.getSensitives()));

        if (wmSensitiveNew != null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }
        wmSensitive.setCreatedTime(new Date());
        save(wmSensitive);
        return ResponseResult.okResult(wmSensitive);
    }

    /**
     * 删除敏感词
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult del(Integer id) {
        removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 更新
     *
     * @param wmSensitive
     * @return
     */
    @Override
    public ResponseResult updateSensitive(WmSensitive wmSensitive) {
        wmSensitive.setCreatedTime(new Date());
        updateById(wmSensitive);
        return ResponseResult.okResult(wmSensitive);
    }

    /**
     * 敏感词校验
     */
    @Override
    public boolean isSensitivePass(String content) {
        boolean flag = true;
        List<WmSensitive> wmSensitives = list();
        List<String> wmSensitiveNames = wmSensitives.stream().map(WmSensitive::getSensitives).collect(Collectors.toList());
        cacheService.set(WemediaConstants.WM_SENSITIVE, JSON.toJSONString(wmSensitiveNames));
        //初始化
        SensitiveWordUtil.initMap(wmSensitiveNames);
        Map<String, Integer> map = SensitiveWordUtil.matchWords(content);
        if (map.size() > 0) flag = false;
        return flag;
    }
}
